import { defineStore } from "pinia";
import {state} from "vue-tsc/out/shared";
export const useChapterStore = defineStore({
  id: "chapterStore",
  state: () => ({
    chapterJson: {}
  }),
  actions: {
    addChapter(content,key) {
      this.chapterJson[key]=content[key]
    },
    removeChapter(id) {
      this.chapterJson = this.chapterJson.filter(chapterJson => chapterJson.id !== id);
    },
    getChapterById(id) {
      // console.log(typeof this.chapterJson);
      if (localStorage.getItem("chapterStore") != null) {
        let chapterContent = JSON.parse(localStorage.getItem("chapterStore")).chapterJson;
        console.log(chapterContent);
        const key = "chapter"+String(id)
        // 判断是否存在 key
        if(chapterContent.hasOwnProperty(key)){
          return chapterContent.key;
        }else {
          return []
        }
      }
      return [];
    }
  },
  persist: {
    strategies: [
      {
        key: "chapterStore",
        storage: localStorage
      }
    ],
    enabled: true
  } // 开启本地缓存
});
